package benny.ApkposStore.Adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import benny.ApkposStore.R;
import benny.ApkposStore.model.DishNew;
import benny.ApkposStore.utils.ApkUntils;
import benny.ApkposStore.utils.Rotate3dView;

public class dishpotlistAdapter extends BaseAdapter {

	private Context context;
	private List<DishNew> list;
	private LayoutInflater mInflater;
	//private TextPaint tp = null;

	public dishpotlistAdapter(Context context, List<DishNew> list) {
		this.context = context;
		this.list = list;
		mInflater = LayoutInflater.from(this.context);
	}

	public int getCount() {
		// TODO Auto-generated method stub
		if (list != null)
			return list.size();
		else
			return 0;
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if (list != null)
			return list.get(position);
		else
			return null;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressWarnings("unused")
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Dishpotlistholder dishlistholder = null;

		if (dishlistholder == null) {
			dishlistholder = new Dishpotlistholder();
			convertView = mInflater.inflate(R.layout.dishpotlistitem, null);
			dishlistholder.dishitem1 = (TextView) convertView
					.findViewById(R.id.dishitem1);
			dishlistholder.dishitem2 = (TextView) convertView
					.findViewById(R.id.dishitem2);
			dishlistholder.dishitem3 = (TextView) convertView
					.findViewById(R.id.dishitem3);
			dishlistholder.dishitem4 = (TextView) convertView
					.findViewById(R.id.dishitem4);
			dishlistholder.dishitem5 = (TextView) convertView
					.findViewById(R.id.dishitem5);
			dishlistholder.dishitem6 = (TextView) convertView
					.findViewById(R.id.dishitem6);
			dishlistholder.dishitem7 = (TextView) convertView
					.findViewById(R.id.dishitem7);
			dishlistholder.dishitem8 = (TextView) convertView
					.findViewById(R.id.dishitem8);
			dishlistholder.dishitem9 = (TextView) convertView
					.findViewById(R.id.dishitem9);
			dishlistholder.dishitem10 = (TextView) convertView
					.findViewById(R.id.dishitem10);
			dishlistholder.dishitem11 = (Rotate3dView) convertView
					.findViewById(R.id.dishitem11);
			dishlistholder.dishitem12 = (Rotate3dView) convertView
					.findViewById(R.id.dishitem12);

			convertView.setTag(dishlistholder);
		} else {
			dishlistholder = (Dishpotlistholder) convertView.getTag();
		}
		float dish_num = list.get(position).getDish_num();
		dishlistholder.dishitem1.setText(list.get(position).getId()+"");
	
		dishlistholder.dishitem3.setText(dish_num+"");
		dishlistholder.dishitem4.setText(list.get(position).getDish_unit()
				.toString());
		dishlistholder.dishitem5.setText(list.get(position).getDish_price()*0.01+"");
		if (list.get(position).getOrder_state()==1) {
			dishlistholder.dishitem6.setText("√");
			
			if (dish_num<=0) {
				changeground(R.color.red, position,
						dishlistholder);	
			}else {
				changeground(R.color.blue, position,
						dishlistholder);	
			}
//			dishlistholder.dishitem11.setVisibility(View.INVISIBLE);
//			dishlistholder.dishitem11.setParentWidth(35);
//			dishlistholder.dishitem11.setParentHeight(23);
//			dishlistholder.dishitem11.setRotateImage(R.drawable.del2,R.drawable.del2);
//
//			dishlistholder.dishitem12.setVisibility(View.INVISIBLE);
//			dishlistholder.dishitem12.setParentWidth(35);
//			dishlistholder.dishitem12.setParentHeight(23);
//			dishlistholder.dishitem12.setRotateImage(R.drawable.del1,R.drawable.del1);
	

		} else {

			dishlistholder.dishitem6.setText("×");
			changeground(R.color.transparent, position,
					dishlistholder);
			
//			dishlistholder.dishitem12.setVisibility(View.VISIBLE);
//			dishlistholder.dishitem12.setParentWidth(35);
//			dishlistholder.dishitem12.setParentHeight(23);
//			dishlistholder.dishitem12.setRotateImage(R.drawable.del1,R.drawable.del1);
//			dishlistholder.dishitem11.setVisibility(View.VISIBLE);
//			dishlistholder.dishitem11.setParentWidth(35);
//			dishlistholder.dishitem11.setParentHeight(23);
//			dishlistholder.dishitem11.setRotateImage(R.drawable.del2,R.drawable.del2);

		}
		
		dishlistholder.dishitem12.setVisibility(View.VISIBLE);
		dishlistholder.dishitem12.setParentWidth(35);
		dishlistholder.dishitem12.setParentHeight(23);
		dishlistholder.dishitem12.setRotateImage(R.drawable.del1,R.drawable.del1);
		dishlistholder.dishitem11.setVisibility(View.VISIBLE);
		dishlistholder.dishitem11.setParentWidth(35);
		dishlistholder.dishitem11.setParentHeight(23);
		dishlistholder.dishitem11.setRotateImage(R.drawable.del2,R.drawable.del2);

		dishlistholder.dishitem7.setText(list.get(position).getCook_extra()+"");
		dishlistholder.dishitem8.setText(list.get(position).getDcy_name());
		double dic=list.get(position).getDic_rate();
		if (dic>=100) {
			dishlistholder.dishitem9.setText("");
		}else {
			dishlistholder.dishitem9.setText(ApkUntils.convertFloat(dic*0.1+"",1));
		}
			
		String dishtime = list.get(position).getDish_time();
		if (dishtime==null) {
			dishlistholder.dishitem10.setText("");
			
		}else {
			dishlistholder.dishitem10.setText(dishtime.substring(
					dishtime.indexOf(" ") + 1, dishtime.length()));
			
		}
		
		int free = list.get(position).getFree_state();
		String dnam_var = list.get(position).getDnam_var();
		
		if (free==0) {
			dishlistholder.dishitem2.setText(dnam_var);	
		}else {
			dishlistholder.dishitem2.setText("(赠送)"+dnam_var);	
		}

			dishlistholder.dishtotal = (float) ((list.get(position).getDish_num())
				* (list.get(position).getDish_price()*0.01));
			

		return convertView;
	}

	public final class Dishpotlistholder {
		public TextView dishitem1;
		public TextView dishitem2;
		public TextView dishitem3;
		public TextView dishitem4;
		public TextView dishitem5;
		public TextView dishitem6;
		public TextView dishitem7;
		public TextView dishitem8;
		public TextView dishitem9;
		public TextView dishitem10;
		public Rotate3dView dishitem11;
		public Rotate3dView dishitem12;
		public float dishtotal;

	}

	public void changeground(int Id, int position, Dishpotlistholder dishlistholder) {
		TextView[] ids = new TextView[] { dishlistholder.dishitem1,
				dishlistholder.dishitem2, dishlistholder.dishitem3,
				dishlistholder.dishitem4, dishlistholder.dishitem5,
				dishlistholder.dishitem6, dishlistholder.dishitem7,
				dishlistholder.dishitem8, dishlistholder.dishitem9,
				dishlistholder.dishitem10 };
		for (int i = 0; i < ids.length; i++) {
			ids[i].setBackgroundResource(Id);
		}

	}
}